var express = require("express");
var app = express();
var bodyParser = require("body-parser")

app.use(
    bodyParser.urlencoded({
        extended: true
    }));
app.use(bodyParser.json())
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

const userLogin = require("./data/user_login.json");
const adminLogin = require("./data/admin_login.json")

const adminPermission = require("./data/admin.json")
const userPermission = require("./data/作业小组.json")

app.post('/login', async (req, res) => {
    let { id, psd } = req.body;
    if (id == 'admin' && psd == 123456) {
        res.send(adminLogin)
    }
    else if (id == 'user' && psd == 123456) {
        res.send(userLogin)
    }
    else {
        res.send('ERROR')
    }
})

app.get("/permission", (req, res) => {
    const role = req.query.role;
    if (role == 'admin_token') {
        res.send(adminPermission)
    }
    if (role == 'user_token') {
        res.send(userPermission)
    }
})

app.listen(1212, '0.0.0.0', () => {
    console.log("http://localhost:1212");
})